﻿using CrankShaftAnalysisSystem.Controller;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CrankShaftAnalysisSystem.SubForm
{
    public partial class FormMeasuring : Form
    {
        public event Action OnComplete;
        public FormMeasuring()
        {
            InitializeComponent();
        }

        private void FormMeasuring_FormClosing(object sender, FormClosingEventArgs e)
        {
            Hide();
            e.Cancel = true;
        }

        public void RefreshDataViewer(ObservableCollection<DataStorage.CurvePoint> points)
        {
            uiDataGrid.DataSource = null;
            uiDataGrid.DataSource = points.ToArray();
            uiDataGrid.Refresh();
        }

        private void FormMeasuring_Load(object sender, EventArgs e)
        {
            tmLoop.Start();
        }

        private void btnViewData_Click(object sender, EventArgs e)
        {
            RefreshDataViewer(MeasureSystem.Instance.DataBase.Clouds);
        }

        private void tmLoop_Tick(object sender, EventArgs e)
        {
            btnViewData.Enabled = MeasureSystem.Instance.MeasureState != MeasureSystem.State.BUSY;
            if(!btnComplete.Enabled)
                lbMeasureTime.Text = (DateTime.Now - MeasureSystem.Instance.StartTime).ToString();
            lbMeasureState.Text = MeasureSystem.Instance.CurrentTaskName;
            measureProcess.Value = (int)Math.Ceiling(MeasureSystem.Instance.Process);
            btnComplete.Enabled = MeasureSystem.Instance.MeasureState == MeasureSystem.State.FINISHED;
        }

        private void btnManualStop_Click(object sender, EventArgs e)
        {
            MeasureSystem.Instance.StopMeasuring();
            SafeController.StopAll();
        }

        private void btnComplete_Click(object sender, EventArgs e)
        {
            if (OnComplete != null)
                OnComplete();
        }
    }
}
